package nc.web.erm.bill.fysq;

import nc.bs.erm.util.CacheUtil;
import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.framework.common.NCLocator;
import nc.bs.logging.Logger;
import nc.bs.pf.pub.PfDataCache;
import nc.itf.arap.prv.IBXBillPrivate;
import nc.itf.fi.pub.Currency;
import nc.vo.er.djlx.DjLXVO;
import nc.vo.erm.fysq.FYSQBillVO;
import nc.vo.erm.matterapp.AggMatterAppVO;
import nc.vo.erm.matterapp.MatterAppVO;
import nc.vo.erm.matterapp.MtAppDetail1VO;
import nc.vo.erm.matterapp.MtAppDetailVO;
import nc.vo.pub.AggregatedValueObject;
import nc.vo.pub.BusinessException;
import nc.vo.pub.SuperVO;
import nc.vo.pub.billtype.BilltypeVO;
import nc.vo.pub.lang.UFDate;
import nc.vo.pub.lang.UFDouble;
import nc.vo.pubapp.pattern.model.entity.bill.AbstractBill;
import nc.web.erm.bill.pub.AbstractBillDefValue;

import org.codehaus.jettison.json.JSONObject;



public class FYSQBillDefValue extends AbstractBillDefValue {
	String[] result;
	DjLXVO[] djlxvos;
	String pk_currency = null;
	UFDouble orgRate;
	UFDouble groupRate;
	UFDouble globalRate;
	String Pk_billtypeid;


	public FYSQBillDefValue(String pk_group, String pk_org, String pk_user, String tradetype, String nodecode) {
		setPk_group(pk_group);
		setPk_user(pk_user);
		setPk_org(pk_org);
		setTradetype(tradetype);
		setNodecode(nodecode);
		try {
			this.result = ((IBXBillPrivate)NCLocator.getInstance().lookup(IBXBillPrivate.class))
					.queryPsnidAndDeptid(InvocationInfoProxy.getInstance().getUserId(), getPk_group());
			this.djlxvos = ((DjLXVO[])CacheUtil.getValueFromCacheByWherePart(DjLXVO.class, " djdl='ma' and DJLXBM='" + tradetype + "' and pk_group='" + 
					InvocationInfoProxy.getInstance().getGroupId() + "' order by djlxbm "));
			if ((this.djlxvos[0] == null) || (this.djlxvos[0].getDefcurrency() == null)) {
				this.pk_currency = Currency.getOrgLocalCurrPK(pk_org);
			} else {
				this.pk_currency = this.djlxvos[0].getDefcurrency();
			}

			this.orgRate = Currency.getRate(pk_org, this.pk_currency, new UFDate());
			this.groupRate = Currency.getGroupRate(pk_org, pk_group, this.pk_currency, new UFDate());
			this.globalRate = Currency.getGlobalRate(pk_org, this.pk_currency, new UFDate());
			BilltypeVO billtypevo = PfDataCache.getBillTypeInfo(this.djlxvos[0].getDjlxbm());
			this.Pk_billtypeid = billtypevo.getPk_billtypeid();
		} catch (BusinessException e) {
			Logger.error(e);
		}
	}


	public AggregatedValueObject getDefaultVO(String pk_org, JSONObject templatedefvjsonString) throws Exception{
		AggregatedValueObject aggvo = getAggVO();
		SuperVO headvo = getHeadVO();

		headvo.setAttributeValue("pk_org", pk_org);
		headvo.setAttributeValue("pk_group", getPk_group());

		setSpecialHeadValue(headvo);
		aggvo.setParentVO(headvo);

		SuperVO childrenVO = getItemVO();
		setSpecialBodyValue(childrenVO);
		dealTemplateDefaultValueJson(headvo, childrenVO, templatedefvjsonString);
		((AbstractBill) aggvo).setChildren(childrenVO.getMetaData(),new SuperVO[] { childrenVO });

		SuperVO fjchildrenVO = getFjItemVO();
		((AbstractBill) aggvo).setChildren(fjchildrenVO.getMetaData(),new SuperVO[] { fjchildrenVO });

		return aggvo;

	}

	protected void setSpecialBodyValue(SuperVO childrenVO) {
		MtAppDetailVO detail = (MtAppDetailVO)childrenVO;
		String pk_psndoc = this.result[0];
		String pk_dept = this.result[1];
		String pk_org = this.result[2];
		detail.setPk_group(getPk_group());
		detail.setPk_org(pk_org);
		detail.setAssume_org(pk_org);
		detail.setApply_dept(pk_dept);
		detail.setAssume_dept(pk_dept);
		detail.setPk_billtype("261X");
		detail.setBilldate(new UFDate());
		detail.setBillmaker(pk_psndoc);
		detail.setBillstatus(Integer.valueOf(1));
		detail.setEffectstatus(Integer.valueOf(0));
		detail.setClose_status(Integer.valueOf(2));
		detail.setPk_tradetype(getTradetype());
		detail.setPk_currtype(this.pk_currency);
		detail.setOrg_currinfo(this.orgRate);
		detail.setGroup_currinfo(this.groupRate);
		detail.setGlobal_currinfo(this.globalRate);

		String[] bodyAmounts = AggMatterAppVO.getBodyAmounts();
		for (String field : bodyAmounts) {
			detail.setAttributeValue(field, UFDouble.ZERO_DBL);
		}
	}


	protected void setSpecialHeadValue(SuperVO headvo) {
		MatterAppVO head = (MatterAppVO)headvo;
		String pk_psndoc = this.result[0];
		String pk_dept = this.result[1];
		String pk_org = this.result[2];
		head.setApply_org(pk_org);
		head.setApply_dept(pk_dept);
		head.setBillmaker(pk_psndoc);
		head.setPk_billtype("261X");
		head.setBilldate(new UFDate());
		head.setPk_group(getPk_group());
		head.setPk_org(getPk_org());
		head.setCreator(pk_psndoc);
		head.setBillstatus(Integer.valueOf(1));
		head.setApprstatus(Integer.valueOf(-1));
		head.setEffectstatus(Integer.valueOf(0));
		head.setClose_status(Integer.valueOf(2));
		head.setPk_tradetype(this.djlxvos[0].getDjlxbm());
		head.setPk_tradetypeid(this.Pk_billtypeid);
		head.setPk_currtype(this.pk_currency);

		String[] headAmounts = AggMatterAppVO.getHeadAmounts();
		for (String field : headAmounts) {
			head.setAttributeValue(field, UFDouble.ZERO_DBL);
		}
		head.setOrg_currinfo(this.orgRate);
		head.setGroup_currinfo(this.groupRate);
		head.setGlobal_currinfo(this.globalRate);
	}


	public void resetHeadDigit(MatterAppVO head) {
		String pk_currtype = head.getPk_currtype();
		String pk_org = head.getPk_org();
		String pk_group = head.getPk_group();
		if ((pk_org == null) || (pk_org.trim().length() == 0))
			return;
		if ((pk_currtype != null) && (pk_currtype.trim().length() > 0)) {
			int hlPrecision = 0;
			int globalhlPrecision = 0;
			int i = 0;
		}
	}



	protected SuperVO getItemVO() {
		MtAppDetailVO childrenVO = new MtAppDetailVO();
		return childrenVO;
	}


	protected SuperVO getFjItemVO() {
		MtAppDetail1VO childrenVO = new MtAppDetail1VO();
		return childrenVO;
	}


	protected SuperVO getHeadVO() {
		MatterAppVO headvo = new MatterAppVO();
		return headvo;
	}

	protected AggregatedValueObject getAggVO() {
		FYSQBillVO aggvo = new FYSQBillVO();
		return aggvo;
	}
}